﻿Imports System.IO
Imports ICSharpCode.SharpZipLib.Tar
Imports System.Text.RegularExpressions

Public Module extentions_pathsAndFiles
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddDirectoryFilesToTar(ByVal archive As TarArchive, ByVal sourceDirectory As String, ByVal recurse As Boolean)

        ' Recursively add sub-folders
        If recurse Then
            Dim directories As String() = Directory.GetDirectories(sourceDirectory)
            For Each _directory As String In directories
                AddDirectoryFilesToTar(archive, _directory, recurse)
            Next
        End If

        ' Add files
        Dim filenames As String() = Directory.GetFiles(sourceDirectory)
        For Each filename As String In filenames
            Dim _tarEntry As TarEntry = TarEntry.CreateEntryFromFile(filename)
            archive.WriteEntry(_tarEntry, True)
        Next
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Function getDateFromName(fic As FileInfo) As Date
        Dim outDate As Date = Nothing
        Try
            Dim format = "yyyyMMdd"

            Dim m = Regex.Match(fic.Name, "^.*(?<test>(19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])).*$", RegexOptions.RightToLeft)

            If m.Success Then
                Dim valeur = m.Groups("test").Value
                outDate = DateTime.ParseExact(valeur, format, Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None)
            Else
                Dim m2 = Regex.Match(fic.Name, "^.*(?<test>\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])).*$", RegexOptions.RightToLeft)

                If m2.Success Then
                    Dim valeur = m2.Groups("test").Value
                    outDate = DateTime.ParseExact("20" & valeur, format, Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None)
                End If
            End If
        Catch
        End Try

        Return outDate
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function getDateFromName(dir As DirectoryInfo) As Date
        Dim outDate As Date = Nothing
        Try
            Dim format = "yyyyMMdd"

            Dim m = Regex.Match(dir.Name, "^.*(?<test>(19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])).*$", RegexOptions.RightToLeft)

            If m.Success Then
                Dim valeur = m.Groups("test").Value
                outDate = DateTime.ParseExact(valeur, format, Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None)
            Else
                Dim m2 = Regex.Match(dir.Name, "^.*(?<test>\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])).*$", RegexOptions.RightToLeft)

                If m2.Success Then
                    Dim valeur = m2.Groups("test").Value
                    outDate = DateTime.ParseExact("20" & valeur, format, Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None)
                End If
            End If

        Catch
        End Try

        Return outDate
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub purge(directory As System.IO.DirectoryInfo)
        For Each file As System.IO.FileInfo In directory.GetFiles()
            file.Delete()
        Next
        For Each subDirectory As System.IO.DirectoryInfo In directory.GetDirectories()
            subDirectory.Delete(True)
        Next
    End Sub

End Module